package com.gk.panda.gateway.util;

import com.alibaba.fastjson.JSONObject;
import com.gk.panda.commons.result.Result;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

public class ResponseUtil {

    public static Mono<Void> writeResponse(ServerHttpResponse response,Object result) {
        DataBufferFactory dataBufferFactory = response.bufferFactory();
        byte[] bytes = JSONObject.toJSONString(Result.ok(result)).getBytes();
        DataBuffer dataBuffer = dataBufferFactory.wrap(bytes);
        return response.writeWith(Mono.just(dataBuffer));
    }

    public static Mono<Void> writeError(ServerHttpResponse response,int status,String message){
        DataBufferFactory dataBufferFactory = response.bufferFactory();
        byte[] bytes = JSONObject.toJSONString(Result.write(status, message)).getBytes();
        DataBuffer dataBuffer = dataBufferFactory.wrap(bytes);
        return response.writeWith(Mono.just(dataBuffer));
    }
}
